package com.ma.controller;

import com.ma.Utils.filesUtil;
import com.ma.entity.Files;

import com.ma.server.FilesServer;
import com.ma.server.LoginServer;
import com.ma.server.StudentServer;
import org.apache.commons.io.FilenameUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

import java.util.Date;
import java.util.List;

/**
 * @ProjectName: project
 * @Package: com.ma.controller
 * @ClassName: FilesCon
 * @Author: YHHHH
 * @Description:
 * @Date: 2021/3/31 20:25
 * @Version: 1.0
 */
@RestController
@CrossOrigin
public class FilesCon {

    @Autowired
    private FilesServer filesServer;
    @Autowired
    private LoginServer loginServer;
    @Autowired
    private StudentServer studentServer;
    @Autowired
    private filesUtil filesUtil;



    @GetMapping("/files/findFiles")
    public List<Files> findFiles(@RequestParam("number")String  number){
        List<Files> byNumber = filesServer.findByNumber(number);
        return byNumber;
    }

    /*
    加文件
     */
    @PostMapping("/files/addFiles")
    @Transactional
    public String addFiles(@RequestParam("files") List<MultipartFile> files, HttpServletRequest request) throws Exception {
        String number = request.getHeader("token");                 //上传的学生学号
        Integer teamID = studentServer.findTeamID(number);
        System.out.println(files.size());
        for (MultipartFile file : files) {
            System.out.println(file);
            String originalFilename = file.getOriginalFilename();       //获取文件全名；
            String baseName = FilenameUtils.getBaseName(originalFilename);  //获取名字；
            long size = file.getSize();                     //获取大小
            String contentType = file.getContentType();     //获取类型
            String extension = FilenameUtils.getExtension(originalFilename);       //拿到扩展名
            String realPath = ResourceUtils.getURL("classpath:").getPath() + "flies";
            String dirPath = realPath + "/" + number;    //文件路径
            File dateDir = new File(dirPath);
            if(!dateDir.exists()){
                dateDir.mkdirs();
            }
            //判断该文件是否存在
            File hasFile=new File(dateDir,originalFilename);
            if(hasFile.exists()) {
                return "您已上传过该文件！";
            }
            file.transferTo(hasFile);
            Date date =new Date();
            //放进数据库
            Files newFile = new Files();
            newFile.setFilesName(baseName);
            newFile.setStudentId(number);
            newFile.setSize(size);
            newFile.setType(extension);
            newFile.setUrl(dirPath);
            newFile.setUploadTime(date);
            newFile.setTeamId(teamID);
            newFile.setDowncounts(0);
            Integer integer = filesServer.addFiles(newFile);
        }
        return "ok";
    }

    /*
    下载文件
     */
    @GetMapping("/files/downloadFiles")
    public String downloadFiles(@RequestParam("id")Integer id,HttpServletResponse response) throws IOException {
        String s = filesUtil.filesOpe(id, response, 1);
        if (s!=null){
            return "success";
        }else {
            return null;
        }
    }
    @GetMapping("/files/onlineFiles")
    public String onlineFiles(@RequestParam("id")Integer id,HttpServletResponse response) throws IOException {
        filesUtil.filesOpe(id,response,2);
        return null;
    }

    /*
    文件删除
     */
    @GetMapping("/files/removeFiles")
    @Transactional
    public String removeFiles(@RequestParam("id")Integer id){
        filesUtil.filesDelete(id);
        Integer integer = filesServer.removeFiles(id);      //从数据库删除
        if (integer!=0){
            return "success";
        }else {
            return "fail";
        }
    }
    /*
    找到小组的全部文件
     */
    @GetMapping("/files/findTeamFiles")
    public List<Files> findTeamFiles(@RequestParam("teamID")Integer teamId){
        List<Files> teamFiles = filesServer.findTeamFiles(teamId);
        return teamFiles;
    }
}
